//
//  SEGInviteRecordViewController.m
//  ExcellentHome
//
//  Created by 揭婧雯 on 2017/2/27.
//  Copyright © 2017年 陈区. All rights reserved.
//

#import "SEGInviteRecordViewController.h"
#import "SEGInviteRecordCell.h"
#import "SEGInviteRecordCountCell.h"
#import "SEGInviteRecordViewModel.h"

@interface SEGInviteRecordViewController ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) UITableView* tableView;
@property (nonatomic, copy) NSString *pageNo;

@property(nonatomic, strong) SEGInviteRecordViewModel *viewModel;
@end

@implementation SEGInviteRecordViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"邀请记录";
    // 列表设置
    self.tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.backgroundColor = kColor_W;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    [self.tableView registerClass:[SEGInviteRecordCountCell class] forCellReuseIdentifier:kInviteRecordCountCellIdentifier];
    [self.tableView registerClass:[SEGInviteRecordCell class] forCellReuseIdentifier:kInviteRecordCellIdentifier];
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cellId"];
    
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker* make) {
        make.left.equalTo(self.view.mas_left);
        make.right.equalTo(self.view.mas_right);
        make.bottom.equalTo(self.view.mas_bottom);
        make.top.equalTo(self.view.mas_top);
    }];
    
    self.viewModel.recordsArr = [NSMutableArray array];
    [self setupRefresh];
    
    self.pageNo = @"1";
    [self loadData];
}

#pragma mark -
#pragma mark 下拉刷新
//===============下拉刷新===========================
- (void)setupRefresh
{
    WeakSelf;
    [self.tableView addWindmillWithFooterRefreshBlock:^{
        [weakSelf footerRereshing];
    }];
}
#pragma mark 开始进入刷新状态
- (void)footerRereshing
{
    if (((int)[self.pageNo integerValue] + 1) > [self.viewModel.recordModel.totalPage integerValue]) {
        [SEGAlertView showTmepMessageWithMessage:@"没有更多数据了"];
        [self.tableView stopAnimatingWindmill];
        return;
    }
    self.pageNo = [NSString stringWithFormat:@"%d", (int)[self.pageNo integerValue] + 1];
    [self loadData];
}

- (void)loadData
{
    [self.viewModel requesthareRecordListWithPageNo:self.pageNo completed:^(NSString * _Nonnull codeType, NSString * _Nonnull errorMessage) {
        [self.tableView stopAnimatingWindmill];
        if ([codeType isEqualToString:@"0"]) {
            self.tableView.hidden = NO;
            [self.tableView reloadData];
        }else {
            self.tableView.hidden = YES;
            [SEGAlertView showTmepMessageWithMessage:errorMessage];
        }
    }];
}

#pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView*)tableView
{
    return 2;
}

- (NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0) {
        return 2;
    }
    else {
        return self.viewModel.recordsArr.count + 2;
    }
}

- (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
    NSInteger section = indexPath.section;
    NSInteger row = indexPath.row;
    if (row == 0) {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cellId" forIndexPath:indexPath];
        cell.textLabel.textColor = kColor_1;
        cell.textLabel.font = [UIFont systemFontOfSize:13];
        if (section == 0) {
            cell.textLabel.text = @"邀请人数统计";
        }else {
            cell.textLabel.text = @"统计详情";
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }
    if (section == 0) {
        SEGInviteRecordCountCell* cell = [tableView dequeueReusableCellWithIdentifier:kInviteRecordCountCellIdentifier forIndexPath:indexPath];
        if (self.viewModel.recordModel) {
            [cell updateWithModel:self.viewModel.recordModel];
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }
    else {
        SEGInviteRecordCell* cell = [tableView dequeueReusableCellWithIdentifier:kInviteRecordCellIdentifier forIndexPath:indexPath];
        if (row == 1) {//调整UI为垂直居中
            [cell updateUIToCenterY];
        }else {
            if (self.viewModel.recordModel) {
                [cell updateWithDetailModel:self.viewModel.recordsArr[row-2]];
            }
        }
        if (row == self.viewModel.recordsArr.count + 1) {
            cell.bottomLine.hidden = NO;
        }else {
            cell.bottomLine.hidden = YES;
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }
}

#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView*)tableView heightForRowAtIndexPath:(NSIndexPath*)indexPath
{
    if (indexPath.row == 0) {
        return 44;
    }
    if (indexPath.section == 0) {
        return 65;
    }
    else {
        if (indexPath.row == 1) {//项目标题
            return 30;
        }else if (indexPath.row == self.viewModel.recordsArr.count+1) {//最后一行加高
            return 40;
        }
        else {
           return 25;
        }
    }
}

- (CGFloat)tableView:(UITableView*)tableView heightForFooterInSection:(NSInteger)section
{
    if (section == 0) {
        return 10;
    }
    else {
        return 0.01;
    }
}

- (UIView*)tableView:(UITableView*)tableView viewForFooterInSection:(NSInteger)section
{
    if (section == 0) {
        UIView* footView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, LYScreenWidth(), 10)];
        footView.backgroundColor = kColor_bg;
        return footView;
    }
    else {
        return [[UIView alloc] initWithFrame:CGRectZero];
    }
}

//设置分割线
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    UIEdgeInsets insets;
    if (indexPath.section == 0) {
        insets = UIEdgeInsetsZero;
    }else {
        if (indexPath.row == 0 || indexPath.row == 1) {
            insets = UIEdgeInsetsZero;
        }else {
            insets = UIEdgeInsetsMake(0, 0, 0, LYScreenWidth());
        }
    }
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
        [cell setSeparatorInset:insets];
    }
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:insets];
    }
    if ([cell respondsToSelector:@selector(setPreservesSuperviewLayoutMargins:)]) {
        [cell setPreservesSuperviewLayoutMargins:NO];
    }
    if ([tableView respondsToSelector:@selector(setSeparatorInset:)]) {
        [tableView setSeparatorInset:insets];
    }
    if ([tableView respondsToSelector:@selector(setLayoutMargins:)]) {
        [tableView setLayoutMargins:insets];
    }
}

#pragma mark - init Views
-(void)segInitViews{

}

#pragma mark - RAC Data Binding
- (void)bindViewModel {
}

#pragma mark - getter
-(SEGInviteRecordViewModel *)viewModel{
    if (!_viewModel) {
        _viewModel = [[SEGInviteRecordViewModel alloc]init];
    }
    return _viewModel;
}
@end
